clear

set more off
set mem 100m


local EVENT  "decevent" /*compevent or decevent*/
local thintrade    = 0 /*0: Do not deal with thin trading issues, 1 deal with  them using trade-to-trade returns*/
local controlevents = 1  /*Whether to include dummies for the corporate events occuring during the relevant periods*/
local controlindustry = 1 /*Whether to include a industrial index in the baseline model (improves the fit significanlty*/
local robust = 1 /*Use longest estimation window (There are 1-7 for the non-thin trade case only)*/
local estwindow 	= 180 /*Calender days*/
local evtwindow 	= 30 /*Calender days: this does not define the length of the window for the actual estimation, just for calling the file. I'm using a window of 10 in line 42*/

*use Data_Event_Study_final_thin`thintrade'_event`controlevents'_ind`controlindustry'_`EVENT', clear
use Data_Event_Study_final_thin`thintrade'_est`estwindow'_evt`evtwindow'_control`controlevents'_ind`controlindustry'_`EVENT', clear
///DROP SAMPLES WITH SHORT ESTIMATION WINDOW
egen tmp1 = min(event_time), by(panel_id)
drop if tmp1>-60 
drop tmp*
drop if event_time<-122 | event_time==. /*To be comparable to the portfolio approach*/
if "`EVENT'"=="majorinitiative1" | "`EVENT'"=="majorinitiative1summit" {
	drop if ticker=="GND" & eventyear==1996
}


if `thintrade'== 0 {
	local thin = ""
}
else if `thintrade'== 1 {
	local thin = "tt"
}



keep panel_id estimation_window event_window event_time AbnormalReturns`thin' AbnormalReturns`thin'd eventyear
drop if (estimation_window==. & event_window==.) | event_time==.



******W/ DIVIDENDS*******
sort panel_id event_time

by panel_id: gen cumAbnormalReturns`thin'd = sum(AbnormalReturns`thin'd)

egen Ndays = count(AbnormalReturns`thin'd), by (panel_id)

egen medAR = median(AbnormalReturns`thin'd), by(panel_id)

gen S_it = .

replace S_it = 1 if AbnormalReturns`thin'd > 0 & AbnormalReturns`thin'd<.
replace S_it = 0 if AbnormalReturns`thin'd <=0

gen cS_it = .

replace cS_it = 1 if cumAbnormalReturns`thin'd > 0 & AbnormalReturns`thin'd<.
replace cS_it = 0 if cumAbnormalReturns`thin'd <= 0


*MEAM DEVIATION & STANDARD DEVIATION

gen stdS_all = .
gen stdS_event = .

egen S_t_all 	= mean(S_it) if estimation_window==1, by(event_time)
egen S_t_event 	= mean(S_it) if estimation_window==1, by(eventyear event_time)

egen tmp= mean(S_it) if estimation_window==1, by(panel_id)
egen S_i	= max(tmp), by(panel_id)
drop tmp

egen p_all 	    = mean(S_i)
egen p_event 	= mean(S_i), by(eventyear)

egen W_all 	    = sum(S_it) if event_window==1, by(event_time)
egen W_event	= sum(S_it) if event_window==1, by(eventyear event_time)

egen w_all 	    = sum(cS_it) if event_window==1, by(event_time)
egen w_event	= sum(cS_it) if event_window==1, by(eventyear event_time)


egen N_all 	    	= count(S_i) , by(event_time)
egen  N_event    	= count(S_i) , by(eventyear event_time)

//Collapsing at the event-year event_day dimension

collapse S_t_*  p_* W_* w_*  N_* estimation_window event_window, by(eventyear event_time)

gen dW_all = W_all - N_all*p_all
gen dw_all = w_all - N_all*p_all

gen dW_event = W_event - N_event*p_event
gen dw_event = w_event - N_event*p_event

gen sd_W_all = (N_all*p_all*(1-p_all))^0.5
gen sd_W_event = (N_event*p_event*(1-p_event))^0.5

gen Z_all	= (W_all - N_all*p_all)/(N_all*p_all*(1-p_all))^0.5
gen cZ_all	= (w_all - N_all*p_all)/(N_all*p_all*(1-p_all))^0.5

gen Z_event	    = (W_event - N_event*p_event)/(N_event*p_event*(1-p_event))^0.5
gen cZ_event	= (w_event - N_event*p_event)/(N_event*p_event*(1-p_event))^0.5

sort eventyear event_time


*OBS: Several variables created in this part of the program are ignored for the table

keep if event_window==1
by eventyear: gen cumdWevent = sum(dW_event) if event_window==1
by eventyear: gen cumdWall = sum(dW_all) if event_window==1


by eventyear: gen T = _n if event_window==1

gen tmp1 = sd_W_event^2
gen tmp2 = sd_W_all^2

by eventyear: gen tmp3 = sum(tmp1) if event_window==1
by eventyear: gen tmp4 = sum(tmp2) if event_window==1

gen sd_cumdWevent = sqrt(tmp3)
gen sd_cumdWall      = sqrt(tmp4)

*SIGN TEST STATISTICS

gen ciu_event = cumdWevent + sd_cumdWevent*1.64
gen cil_event = cumdWevent - sd_cumdWevent*1.64

gen ciu_all = cumdWall + sd_cumdWall*1.64
gen cil_all = cumdWall - sd_cumdWall*1.64

gen pvalue_event = (1-normal(abs(Z_event)))*2
gen pvalue_all = (1-normal(abs(Z_all)))*2


sort eventyear event_time

collapse dW_all sd_W_all, by(event_time)

saveold  Table_6_3, replace

